#include <nds.h>
#include <nds/arm9/trig_lut.h>
#include "sprite.h"

void updateOAM(tOAM * oam){
	DC_FlushAll();
	dmaCopyHalfWords(SpriteDmaChannel,
					 oam->spriteBuffer,
					 OAM,
					 SPRITE_COUNT * sizeof(SpriteEntry));
}

void initOAM(tOAM * oam){
	for ( int i = 0; i < SPRITE_COUNT; i++){
		oam->spriteBuffer[i].attribute[0] = ATTR0_DISABLED;
		oam->spriteBuffer[i].attribute[1] = 0;
		oam->spriteBuffer[i].attribute[2] = 0;
	}
	for ( int i = 0; i < MATRIX_COUNT; i++){
		oam->matrixBuffer[1].hdx = 1 << 8;
		oam->matrixBuffer[1].hdy = 0;
		oam->matrixBuffer[1].vdx = 0;
		oam->matrixBuffer[1].vdy = 1 << 8;
	}
	updateOAM(oam);
}

void rotateSprite(SpriteRotation * spriteRotation, u16 angle){
	s16 s = SIN[angle & SpriteAngleMask] >> 4;
	s16 c = COS[angle & SpriteAngleMask] >> 4;

	spriteRotation->hdx = c;
	spriteRotation->hdy = s;
	spriteRotation->vdx = -s;
	spriteRotation->vdy = c;
}

void setSpriteVisibility(SpriteEntry * spriteEntry,bool hidden, bool affine, bool doubleBound){
	if(hidden){
		spriteEntry->isRotoscale = false;
		spriteEntry->isHidden=true;
	}
	else{
		if(affine){
			spriteEntry->isRotoscale = true;
			spriteEntry->rsDouble = doubleBound;
		}
		else{
			spriteEntry->isHidden = false;
		}
	}
}